package net.vanchu.mortgagecalculator.common.config;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * 选项提供者
 * <p/>
 * Created by Granton on 2016/12/15.
 */
public class OptionsProvider {

    public static final int DISCOUNT_DEFAULT_POSITION = 3;

    private static final HashMap<String, LoanRate> MAP_LOANS_RATE = new HashMap<>();

    public static HashMap<String, LoanRate> getMapLoansRate() {
        return new HashMap<>(MAP_LOANS_RATE);
    }

    private static final HashMap<String, Float> MAP_DISCOUNT = new HashMap<>();

    public static HashMap<String, Float> getMapDiscount() {
        return new HashMap<>(MAP_DISCOUNT);
    }

    private static final HashMap<String, Integer> MAP_YEAR = new HashMap<>();

    public static HashMap<String, Integer> getMapYear() {
        return new HashMap<>(MAP_YEAR);
    }

    private static final HashMap<String, LoanWay> MAP_LOANS_WAY = new HashMap<>();

    public static HashMap<String, LoanWay> getMapLoansWay() {
        return new HashMap<>(MAP_LOANS_WAY);
    }

    private static final List<String> LOANS_WAY_NAMES = new ArrayList<>();

    public static List<String> getLoansWayNames() {
        return new ArrayList<>(LOANS_WAY_NAMES);
    }

    private static final List<String> YEARS_STR = new ArrayList<>();

    public static List<String> getYearsStr() {
        return new ArrayList<>(YEARS_STR);
    }

    static List<String> RATE_NAMES = new ArrayList<>();

    public static List<String> getRateNames() {
        return new ArrayList<>(RATE_NAMES);
    }

    private static final List<String> DISCOUNT_NAMES = new ArrayList<>();

    public static List<String> getDiscountNames() {
        return new ArrayList<>(DISCOUNT_NAMES);
    }

    public static void init(ConfigProvider configProvider) {
        initYearsMap(); // 初始化年份
        initLoansWayNames(configProvider);
        initOptionWays(configProvider);
        initDiscountNames(configProvider); // 折扣名称
        initDiscountMap(configProvider); // 折扣映射表
        initLoansRateKeys(configProvider); // 利率名称
        initCommercialLoansRates(configProvider); // 贷款利率映射表
    }

    private static void initOptionWays(ConfigProvider configProvider) {
        int size = LOANS_WAY_NAMES.size();
        for (int i = 0; i < size; i++) {
            MAP_LOANS_WAY.put(LOANS_WAY_NAMES.get(i), configProvider.provideLoanWays()[i]);
        }
    }

    private static void initLoansWayNames(ConfigProvider configProvider) {
        Collections.addAll(LOANS_WAY_NAMES, configProvider.provideLoansWayNames());
    }

    private static void initYearsMap() {
        for (int i = 1; i < 30; i++) {
            String yearStr = String.format(Locale.getDefault(), "%d年", i);
            YEARS_STR.add(yearStr);
            MAP_YEAR.put(yearStr, i);
        }
    }

    private static void initDiscountMap(ConfigProvider configProvider) {
        int size = DISCOUNT_NAMES.size();
        float[] discountValues = configProvider.provideDiscountValues();
        for (int i = 0; i < size; i++) {
            MAP_DISCOUNT.put(DISCOUNT_NAMES.get(i), discountValues[i]);
        }
    }

    private static void initDiscountNames(ConfigProvider configProvider) {
        String[] discountNames = configProvider.provideDiscountNames();
        Collections.addAll(DISCOUNT_NAMES, discountNames);
    }

    /**
     * 初始化 商业贷款利息哈希表
     *
     * @param configProvider 配置提供者
     */
    private static void initCommercialLoansRates(ConfigProvider configProvider) {
        int size = RATE_NAMES.size();
        for (int i = 0; i < size; i++) {
            LoanRate value = configProvider.provideProLoanRates()[i];
            MAP_LOANS_RATE.put(RATE_NAMES.get(i), value);
        }
    }

    /**
     * 初始化利率的名称
     *
     * @param configProvider 配置提供者
     */
    private static void initLoansRateKeys(ConfigProvider configProvider) {
        Collections.addAll(RATE_NAMES, configProvider.provideLoansRateNames());
    }

    public static String convertLoanWayToStringShow(LoanWay loanWay) {
        for (Map.Entry<String, LoanWay> entry : MAP_LOANS_WAY.entrySet()) {
            if (loanWay == entry.getValue()) {
                return entry.getKey();
            }
        }
        return null;
    }
}